package com.utils.deal;

import java.io.*;

public class JsonStrUtil {

    public static String readTxtFile(){
        StringBuilder sb = new StringBuilder();
        String result = "";
        try {
            File f = new File(JsonStrUtil.class.getResource("/").getPath());
            String filePath = f.getParentFile().getParent()+"\\src\\main\\java\\com\\utils\\deal\\JsonStr.txt";
            String encoding="UTF-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    String [] arrLine = new String[2];
                    if(lineTxt.contains(": ")){
                        arrLine =lineTxt.split(": ");
                    }
                    sb.append("\""+arrLine[0]+"\""+":"+arrLine[1]+",");
                }
                result = sb.toString();
                if(result.endsWith(",")){
                    result = result.substring(0,result.length()-1);
                }
                result = "{"+result+"}" ;
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String json = readTxtFile();
        System.out.println(json);

    }


}
